{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regressão Linear Multivariada - Trabalho\n",
    "\n",
    "## Estudo de caso: Qualidade de Vinhos\n",
    "\n",
    "Nesta trabalho, treinaremos um modelo de regressão linear usando descendência de gradiente estocástico no conjunto de dados da Qualidade do Vinho. O exemplo pressupõe que uma cópia CSV do conjunto de dados está no diretório de trabalho atual com o nome do arquivo *winequality-white.csv*.\n",
    "\n",
    "O conjunto de dados de qualidade do vinho envolve a previsão da qualidade dos vinhos brancos em uma escala, com medidas químicas de cada vinho. É um problema de classificação multiclasse, mas também pode ser enquadrado como um problema de regressão. O número de observações para cada classe não é equilibrado. Existem 4.898 observações com 11 variáveis de entrada e 1 variável de saída. Os nomes das variáveis são os seguintes:\n",
    "\n",
    "1. Fixed acidity.\n",
    "2. Volatile acidity.\n",
    "3. Citric acid.\n",
    "4. Residual sugar.\n",
    "5. Chlorides.\n",
    "6. Free sulfur dioxide. \n",
    "7. Total sulfur dioxide. \n",
    "8. Density.\n",
    "9. pH.\n",
    "10. Sulphates.\n",
    "11. Alcohol.\n",
    "12. Quality (score between 0 and 10).\n",
    "\n",
    "O desempenho de referencia de predição do valor médio é um RMSE de aproximadamente 0.148 pontos de qualidade.\n",
    "\n",
    "Utilize o exemplo apresentado no tutorial e altere-o de forma a carregar os dados e analisar a acurácia de sua solução. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from csv import reader\n",
    "from random import randrange\n",
    "from math import sqrt\n",
    "from sklearn.metrics import accuracy_score, classification_report"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predict(row, coefficients):\n",
    "    yhat = coefficients[0]\n",
    "    for i in range(len(row)-1):\n",
    "        yhat += coefficients[i + 1] * row[i]\n",
    "    return yhat\n",
    "\n",
    "def coefficients_sgd(train, l_rate, n_epoch):\n",
    "    coef = [0.0 for i in range(len(train[0]))]\n",
    "    # print ('Coeficiente Inicial={0}' % (coef))\n",
    "    for epoch in range(n_epoch):\n",
    "        sum_error = 0\n",
    "        for row in train:\n",
    "            yhat = predict(row, coef)\n",
    "            error = yhat - row[-1]\n",
    "            sum_error += error**2\n",
    "            coef[0] = coef[0] - l_rate * error\n",
    "            for i in range(len(row)-1):\n",
    "                coef[i + 1] = coef[i + 1] - l_rate * error * row[i] \n",
    "        #print(('epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error)))\n",
    "    return coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Fixed acidity</th>\n",
       "      <th>Volatile acidity</th>\n",
       "      <th>Citric acid</th>\n",
       "      <th>Residual sugar</th>\n",
       "      <th>Chlorides</th>\n",
       "      <th>Free sulfur dioxide</th>\n",
       "      <th>Total sulfur dioxide</th>\n",
       "      <th>Density</th>\n",
       "      <th>pH</th>\n",
       "      <th>Sulphates</th>\n",
       "      <th>Alcohol</th>\n",
       "      <th>Quality</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7.0</td>\n",
       "      <td>0.27</td>\n",
       "      <td>0.36</td>\n",
       "      <td>20.7</td>\n",
       "      <td>0.045</td>\n",
       "      <td>45.0</td>\n",
       "      <td>170.0</td>\n",
       "      <td>1.0010</td>\n",
       "      <td>3.00</td>\n",
       "      <td>0.45</td>\n",
       "      <td>8.8</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6.3</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.34</td>\n",
       "      <td>1.6</td>\n",
       "      <td>0.049</td>\n",
       "      <td>14.0</td>\n",
       "      <td>132.0</td>\n",
       "      <td>0.9940</td>\n",
       "      <td>3.30</td>\n",
       "      <td>0.49</td>\n",
       "      <td>9.5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8.1</td>\n",
       "      <td>0.28</td>\n",
       "      <td>0.40</td>\n",
       "      <td>6.9</td>\n",
       "      <td>0.050</td>\n",
       "      <td>30.0</td>\n",
       "      <td>97.0</td>\n",
       "      <td>0.9951</td>\n",
       "      <td>3.26</td>\n",
       "      <td>0.44</td>\n",
       "      <td>10.1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7.2</td>\n",
       "      <td>0.23</td>\n",
       "      <td>0.32</td>\n",
       "      <td>8.5</td>\n",
       "      <td>0.058</td>\n",
       "      <td>47.0</td>\n",
       "      <td>186.0</td>\n",
       "      <td>0.9956</td>\n",
       "      <td>3.19</td>\n",
       "      <td>0.40</td>\n",
       "      <td>9.9</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7.2</td>\n",
       "      <td>0.23</td>\n",
       "      <td>0.32</td>\n",
       "      <td>8.5</td>\n",
       "      <td>0.058</td>\n",
       "      <td>47.0</td>\n",
       "      <td>186.0</td>\n",
       "      <td>0.9956</td>\n",
       "      <td>3.19</td>\n",
       "      <td>0.40</td>\n",
       "      <td>9.9</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Fixed acidity  Volatile acidity  Citric acid  Residual sugar  Chlorides  \\\n",
       "0            7.0              0.27         0.36            20.7      0.045   \n",
       "1            6.3              0.30         0.34             1.6      0.049   \n",
       "2            8.1              0.28         0.40             6.9      0.050   \n",
       "3            7.2              0.23         0.32             8.5      0.058   \n",
       "4            7.2              0.23         0.32             8.5      0.058   \n",
       "\n",
       "   Free sulfur dioxide  Total sulfur dioxide  Density    pH  Sulphates  \\\n",
       "0                 45.0                 170.0   1.0010  3.00       0.45   \n",
       "1                 14.0                 132.0   0.9940  3.30       0.49   \n",
       "2                 30.0                  97.0   0.9951  3.26       0.44   \n",
       "3                 47.0                 186.0   0.9956  3.19       0.40   \n",
       "4                 47.0                 186.0   0.9956  3.19       0.40   \n",
       "\n",
       "   Alcohol  Quality  \n",
       "0      8.8        6  \n",
       "1      9.5        6  \n",
       "2     10.1        6  \n",
       "3      9.9        6  \n",
       "4      9.9        6  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('winequality-white.csv',names = ['Fixed acidity','Volatile acidity','Citric acid','Residual sugar','Chlorides','Free sulfur dioxide','Total sulfur dioxide','Density','pH','Sulphates','Alcohol','Quality'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for f in ['Fixed acidity','Volatile acidity','Citric acid','Residual sugar','Chlorides','Free sulfur dioxide','Total sulfur dioxide','Density','pH','Sulphates','Alcohol']:\n",
    "    df[f] = df[f].astype('category')\n",
    "    df[f] = df[f].cat.codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Fixed acidity</th>\n",
       "      <th>Volatile acidity</th>\n",
       "      <th>Citric acid</th>\n",
       "      <th>Residual sugar</th>\n",
       "      <th>Chlorides</th>\n",
       "      <th>Free sulfur dioxide</th>\n",
       "      <th>Total sulfur dioxide</th>\n",
       "      <th>Density</th>\n",
       "      <th>pH</th>\n",
       "      <th>Sulphates</th>\n",
       "      <th>Alcohol</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>31</td>\n",
       "      <td>36</td>\n",
       "      <td>36</td>\n",
       "      <td>302</td>\n",
       "      <td>34</td>\n",
       "      <td>57</td>\n",
       "      <td>146</td>\n",
       "      <td>878</td>\n",
       "      <td>23</td>\n",
       "      <td>22</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23</td>\n",
       "      <td>42</td>\n",
       "      <td>34</td>\n",
       "      <td>17</td>\n",
       "      <td>38</td>\n",
       "      <td>13</td>\n",
       "      <td>106</td>\n",
       "      <td>471</td>\n",
       "      <td>53</td>\n",
       "      <td>26</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>43</td>\n",
       "      <td>38</td>\n",
       "      <td>40</td>\n",
       "      <td>101</td>\n",
       "      <td>39</td>\n",
       "      <td>33</td>\n",
       "      <td>69</td>\n",
       "      <td>560</td>\n",
       "      <td>49</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34</td>\n",
       "      <td>28</td>\n",
       "      <td>32</td>\n",
       "      <td>128</td>\n",
       "      <td>47</td>\n",
       "      <td>59</td>\n",
       "      <td>163</td>\n",
       "      <td>601</td>\n",
       "      <td>42</td>\n",
       "      <td>17</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>34</td>\n",
       "      <td>28</td>\n",
       "      <td>32</td>\n",
       "      <td>128</td>\n",
       "      <td>47</td>\n",
       "      <td>59</td>\n",
       "      <td>163</td>\n",
       "      <td>601</td>\n",
       "      <td>42</td>\n",
       "      <td>17</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>43</td>\n",
       "      <td>38</td>\n",
       "      <td>40</td>\n",
       "      <td>101</td>\n",
       "      <td>39</td>\n",
       "      <td>33</td>\n",
       "      <td>69</td>\n",
       "      <td>560</td>\n",
       "      <td>49</td>\n",
       "      <td>21</td>\n",
       "      <td>24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>22</td>\n",
       "      <td>46</td>\n",
       "      <td>16</td>\n",
       "      <td>103</td>\n",
       "      <td>34</td>\n",
       "      <td>33</td>\n",
       "      <td>110</td>\n",
       "      <td>544</td>\n",
       "      <td>41</td>\n",
       "      <td>24</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>31</td>\n",
       "      <td>36</td>\n",
       "      <td>36</td>\n",
       "      <td>302</td>\n",
       "      <td>34</td>\n",
       "      <td>57</td>\n",
       "      <td>146</td>\n",
       "      <td>878</td>\n",
       "      <td>23</td>\n",
       "      <td>22</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>23</td>\n",
       "      <td>42</td>\n",
       "      <td>34</td>\n",
       "      <td>17</td>\n",
       "      <td>38</td>\n",
       "      <td>13</td>\n",
       "      <td>106</td>\n",
       "      <td>471</td>\n",
       "      <td>53</td>\n",
       "      <td>26</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>43</td>\n",
       "      <td>26</td>\n",
       "      <td>43</td>\n",
       "      <td>15</td>\n",
       "      <td>33</td>\n",
       "      <td>30</td>\n",
       "      <td>102</td>\n",
       "      <td>453</td>\n",
       "      <td>45</td>\n",
       "      <td>22</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>43</td>\n",
       "      <td>36</td>\n",
       "      <td>41</td>\n",
       "      <td>14</td>\n",
       "      <td>22</td>\n",
       "      <td>9</td>\n",
       "      <td>35</td>\n",
       "      <td>194</td>\n",
       "      <td>22</td>\n",
       "      <td>33</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>48</td>\n",
       "      <td>28</td>\n",
       "      <td>40</td>\n",
       "      <td>56</td>\n",
       "      <td>24</td>\n",
       "      <td>17</td>\n",
       "      <td>81</td>\n",
       "      <td>527</td>\n",
       "      <td>37</td>\n",
       "      <td>30</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>41</td>\n",
       "      <td>19</td>\n",
       "      <td>37</td>\n",
       "      <td>9</td>\n",
       "      <td>29</td>\n",
       "      <td>16</td>\n",
       "      <td>47</td>\n",
       "      <td>300</td>\n",
       "      <td>41</td>\n",
       "      <td>40</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>27</td>\n",
       "      <td>15</td>\n",
       "      <td>40</td>\n",
       "      <td>15</td>\n",
       "      <td>33</td>\n",
       "      <td>60</td>\n",
       "      <td>117</td>\n",
       "      <td>230</td>\n",
       "      <td>77</td>\n",
       "      <td>29</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>45</td>\n",
       "      <td>66</td>\n",
       "      <td>62</td>\n",
       "      <td>288</td>\n",
       "      <td>29</td>\n",
       "      <td>49</td>\n",
       "      <td>148</td>\n",
       "      <td>863</td>\n",
       "      <td>21</td>\n",
       "      <td>44</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>27</td>\n",
       "      <td>17</td>\n",
       "      <td>38</td>\n",
       "      <td>15</td>\n",
       "      <td>21</td>\n",
       "      <td>30</td>\n",
       "      <td>84</td>\n",
       "      <td>249</td>\n",
       "      <td>48</td>\n",
       "      <td>32</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>23</td>\n",
       "      <td>77</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>35</td>\n",
       "      <td>33</td>\n",
       "      <td>71</td>\n",
       "      <td>370</td>\n",
       "      <td>47</td>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>22</td>\n",
       "      <td>103</td>\n",
       "      <td>48</td>\n",
       "      <td>9</td>\n",
       "      <td>18</td>\n",
       "      <td>32</td>\n",
       "      <td>47</td>\n",
       "      <td>59</td>\n",
       "      <td>56</td>\n",
       "      <td>16</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>36</td>\n",
       "      <td>50</td>\n",
       "      <td>42</td>\n",
       "      <td>7</td>\n",
       "      <td>22</td>\n",
       "      <td>17</td>\n",
       "      <td>147</td>\n",
       "      <td>274</td>\n",
       "      <td>35</td>\n",
       "      <td>30</td>\n",
       "      <td>49</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>26</td>\n",
       "      <td>44</td>\n",
       "      <td>14</td>\n",
       "      <td>112</td>\n",
       "      <td>33</td>\n",
       "      <td>38</td>\n",
       "      <td>107</td>\n",
       "      <td>593</td>\n",
       "      <td>45</td>\n",
       "      <td>27</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>22</td>\n",
       "      <td>103</td>\n",
       "      <td>48</td>\n",
       "      <td>9</td>\n",
       "      <td>18</td>\n",
       "      <td>32</td>\n",
       "      <td>47</td>\n",
       "      <td>59</td>\n",
       "      <td>56</td>\n",
       "      <td>16</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>24</td>\n",
       "      <td>44</td>\n",
       "      <td>38</td>\n",
       "      <td>39</td>\n",
       "      <td>27</td>\n",
       "      <td>19</td>\n",
       "      <td>74</td>\n",
       "      <td>230</td>\n",
       "      <td>40</td>\n",
       "      <td>12</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>29</td>\n",
       "      <td>34</td>\n",
       "      <td>42</td>\n",
       "      <td>19</td>\n",
       "      <td>38</td>\n",
       "      <td>49</td>\n",
       "      <td>95</td>\n",
       "      <td>387</td>\n",
       "      <td>70</td>\n",
       "      <td>25</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>38</td>\n",
       "      <td>104</td>\n",
       "      <td>14</td>\n",
       "      <td>15</td>\n",
       "      <td>63</td>\n",
       "      <td>27</td>\n",
       "      <td>144</td>\n",
       "      <td>445</td>\n",
       "      <td>28</td>\n",
       "      <td>28</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>27</td>\n",
       "      <td>36</td>\n",
       "      <td>41</td>\n",
       "      <td>11</td>\n",
       "      <td>41</td>\n",
       "      <td>16</td>\n",
       "      <td>116</td>\n",
       "      <td>560</td>\n",
       "      <td>65</td>\n",
       "      <td>24</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>31</td>\n",
       "      <td>32</td>\n",
       "      <td>32</td>\n",
       "      <td>137</td>\n",
       "      <td>35</td>\n",
       "      <td>72</td>\n",
       "      <td>229</td>\n",
       "      <td>593</td>\n",
       "      <td>48</td>\n",
       "      <td>27</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>30</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>5</td>\n",
       "      <td>41</td>\n",
       "      <td>39</td>\n",
       "      <td>120</td>\n",
       "      <td>387</td>\n",
       "      <td>68</td>\n",
       "      <td>21</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>31</td>\n",
       "      <td>38</td>\n",
       "      <td>39</td>\n",
       "      <td>132</td>\n",
       "      <td>40</td>\n",
       "      <td>36</td>\n",
       "      <td>115</td>\n",
       "      <td>641</td>\n",
       "      <td>61</td>\n",
       "      <td>30</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>36</td>\n",
       "      <td>36</td>\n",
       "      <td>48</td>\n",
       "      <td>7</td>\n",
       "      <td>36</td>\n",
       "      <td>17</td>\n",
       "      <td>106</td>\n",
       "      <td>249</td>\n",
       "      <td>42</td>\n",
       "      <td>26</td>\n",
       "      <td>59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>34</td>\n",
       "      <td>46</td>\n",
       "      <td>36</td>\n",
       "      <td>25</td>\n",
       "      <td>22</td>\n",
       "      <td>42</td>\n",
       "      <td>86</td>\n",
       "      <td>175</td>\n",
       "      <td>33</td>\n",
       "      <td>48</td>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4868</th>\n",
       "      <td>17</td>\n",
       "      <td>28</td>\n",
       "      <td>31</td>\n",
       "      <td>62</td>\n",
       "      <td>35</td>\n",
       "      <td>51</td>\n",
       "      <td>97</td>\n",
       "      <td>409</td>\n",
       "      <td>54</td>\n",
       "      <td>41</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4869</th>\n",
       "      <td>27</td>\n",
       "      <td>30</td>\n",
       "      <td>33</td>\n",
       "      <td>155</td>\n",
       "      <td>21</td>\n",
       "      <td>6</td>\n",
       "      <td>53</td>\n",
       "      <td>650</td>\n",
       "      <td>42</td>\n",
       "      <td>28</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4870</th>\n",
       "      <td>20</td>\n",
       "      <td>46</td>\n",
       "      <td>28</td>\n",
       "      <td>95</td>\n",
       "      <td>10</td>\n",
       "      <td>32</td>\n",
       "      <td>106</td>\n",
       "      <td>290</td>\n",
       "      <td>38</td>\n",
       "      <td>13</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4871</th>\n",
       "      <td>9</td>\n",
       "      <td>22</td>\n",
       "      <td>40</td>\n",
       "      <td>23</td>\n",
       "      <td>4</td>\n",
       "      <td>21</td>\n",
       "      <td>70</td>\n",
       "      <td>100</td>\n",
       "      <td>60</td>\n",
       "      <td>32</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4872</th>\n",
       "      <td>19</td>\n",
       "      <td>66</td>\n",
       "      <td>41</td>\n",
       "      <td>186</td>\n",
       "      <td>21</td>\n",
       "      <td>63</td>\n",
       "      <td>156</td>\n",
       "      <td>647</td>\n",
       "      <td>37</td>\n",
       "      <td>37</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4873</th>\n",
       "      <td>16</td>\n",
       "      <td>24</td>\n",
       "      <td>32</td>\n",
       "      <td>17</td>\n",
       "      <td>19</td>\n",
       "      <td>37</td>\n",
       "      <td>95</td>\n",
       "      <td>160</td>\n",
       "      <td>56</td>\n",
       "      <td>29</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4874</th>\n",
       "      <td>15</td>\n",
       "      <td>22</td>\n",
       "      <td>36</td>\n",
       "      <td>33</td>\n",
       "      <td>37</td>\n",
       "      <td>16</td>\n",
       "      <td>98</td>\n",
       "      <td>372</td>\n",
       "      <td>72</td>\n",
       "      <td>26</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4875</th>\n",
       "      <td>36</td>\n",
       "      <td>26</td>\n",
       "      <td>26</td>\n",
       "      <td>9</td>\n",
       "      <td>24</td>\n",
       "      <td>18</td>\n",
       "      <td>69</td>\n",
       "      <td>339</td>\n",
       "      <td>35</td>\n",
       "      <td>18</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4876</th>\n",
       "      <td>22</td>\n",
       "      <td>58</td>\n",
       "      <td>42</td>\n",
       "      <td>33</td>\n",
       "      <td>27</td>\n",
       "      <td>38</td>\n",
       "      <td>90</td>\n",
       "      <td>241</td>\n",
       "      <td>59</td>\n",
       "      <td>36</td>\n",
       "      <td>59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4877</th>\n",
       "      <td>18</td>\n",
       "      <td>85</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>21</td>\n",
       "      <td>11</td>\n",
       "      <td>54</td>\n",
       "      <td>375</td>\n",
       "      <td>48</td>\n",
       "      <td>13</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4878</th>\n",
       "      <td>22</td>\n",
       "      <td>84</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>24</td>\n",
       "      <td>4</td>\n",
       "      <td>53</td>\n",
       "      <td>328</td>\n",
       "      <td>47</td>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4879</th>\n",
       "      <td>27</td>\n",
       "      <td>50</td>\n",
       "      <td>40</td>\n",
       "      <td>121</td>\n",
       "      <td>35</td>\n",
       "      <td>88</td>\n",
       "      <td>146</td>\n",
       "      <td>546</td>\n",
       "      <td>38</td>\n",
       "      <td>27</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4880</th>\n",
       "      <td>27</td>\n",
       "      <td>50</td>\n",
       "      <td>40</td>\n",
       "      <td>121</td>\n",
       "      <td>35</td>\n",
       "      <td>88</td>\n",
       "      <td>146</td>\n",
       "      <td>546</td>\n",
       "      <td>38</td>\n",
       "      <td>27</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4881</th>\n",
       "      <td>9</td>\n",
       "      <td>29</td>\n",
       "      <td>27</td>\n",
       "      <td>176</td>\n",
       "      <td>19</td>\n",
       "      <td>38</td>\n",
       "      <td>91</td>\n",
       "      <td>585</td>\n",
       "      <td>30</td>\n",
       "      <td>27</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4882</th>\n",
       "      <td>14</td>\n",
       "      <td>46</td>\n",
       "      <td>13</td>\n",
       "      <td>11</td>\n",
       "      <td>26</td>\n",
       "      <td>57</td>\n",
       "      <td>130</td>\n",
       "      <td>287</td>\n",
       "      <td>49</td>\n",
       "      <td>15</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4883</th>\n",
       "      <td>8</td>\n",
       "      <td>75</td>\n",
       "      <td>17</td>\n",
       "      <td>23</td>\n",
       "      <td>24</td>\n",
       "      <td>77</td>\n",
       "      <td>122</td>\n",
       "      <td>96</td>\n",
       "      <td>50</td>\n",
       "      <td>12</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4884</th>\n",
       "      <td>26</td>\n",
       "      <td>48</td>\n",
       "      <td>38</td>\n",
       "      <td>125</td>\n",
       "      <td>37</td>\n",
       "      <td>88</td>\n",
       "      <td>150</td>\n",
       "      <td>545</td>\n",
       "      <td>37</td>\n",
       "      <td>27</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4885</th>\n",
       "      <td>27</td>\n",
       "      <td>50</td>\n",
       "      <td>40</td>\n",
       "      <td>121</td>\n",
       "      <td>35</td>\n",
       "      <td>88</td>\n",
       "      <td>146</td>\n",
       "      <td>546</td>\n",
       "      <td>38</td>\n",
       "      <td>27</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4886</th>\n",
       "      <td>22</td>\n",
       "      <td>24</td>\n",
       "      <td>28</td>\n",
       "      <td>82</td>\n",
       "      <td>17</td>\n",
       "      <td>57</td>\n",
       "      <td>94</td>\n",
       "      <td>273</td>\n",
       "      <td>44</td>\n",
       "      <td>78</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4887</th>\n",
       "      <td>22</td>\n",
       "      <td>64</td>\n",
       "      <td>22</td>\n",
       "      <td>23</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>28</td>\n",
       "      <td>64</td>\n",
       "      <td>27</td>\n",
       "      <td>56</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4888</th>\n",
       "      <td>29</td>\n",
       "      <td>26</td>\n",
       "      <td>36</td>\n",
       "      <td>9</td>\n",
       "      <td>41</td>\n",
       "      <td>43</td>\n",
       "      <td>100</td>\n",
       "      <td>414</td>\n",
       "      <td>27</td>\n",
       "      <td>31</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4889</th>\n",
       "      <td>8</td>\n",
       "      <td>29</td>\n",
       "      <td>27</td>\n",
       "      <td>176</td>\n",
       "      <td>19</td>\n",
       "      <td>38</td>\n",
       "      <td>91</td>\n",
       "      <td>585</td>\n",
       "      <td>30</td>\n",
       "      <td>27</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4890</th>\n",
       "      <td>20</td>\n",
       "      <td>50</td>\n",
       "      <td>29</td>\n",
       "      <td>28</td>\n",
       "      <td>25</td>\n",
       "      <td>27</td>\n",
       "      <td>72</td>\n",
       "      <td>71</td>\n",
       "      <td>29</td>\n",
       "      <td>21</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4891</th>\n",
       "      <td>16</td>\n",
       "      <td>24</td>\n",
       "      <td>32</td>\n",
       "      <td>3</td>\n",
       "      <td>27</td>\n",
       "      <td>43</td>\n",
       "      <td>94</td>\n",
       "      <td>188</td>\n",
       "      <td>47</td>\n",
       "      <td>23</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4892</th>\n",
       "      <td>26</td>\n",
       "      <td>28</td>\n",
       "      <td>38</td>\n",
       "      <td>11</td>\n",
       "      <td>21</td>\n",
       "      <td>32</td>\n",
       "      <td>84</td>\n",
       "      <td>385</td>\n",
       "      <td>52</td>\n",
       "      <td>31</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4893</th>\n",
       "      <td>22</td>\n",
       "      <td>24</td>\n",
       "      <td>29</td>\n",
       "      <td>17</td>\n",
       "      <td>28</td>\n",
       "      <td>26</td>\n",
       "      <td>64</td>\n",
       "      <td>224</td>\n",
       "      <td>50</td>\n",
       "      <td>27</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4894</th>\n",
       "      <td>27</td>\n",
       "      <td>46</td>\n",
       "      <td>36</td>\n",
       "      <td>120</td>\n",
       "      <td>36</td>\n",
       "      <td>73</td>\n",
       "      <td>144</td>\n",
       "      <td>544</td>\n",
       "      <td>38</td>\n",
       "      <td>23</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4895</th>\n",
       "      <td>26</td>\n",
       "      <td>30</td>\n",
       "      <td>19</td>\n",
       "      <td>9</td>\n",
       "      <td>30</td>\n",
       "      <td>33</td>\n",
       "      <td>83</td>\n",
       "      <td>347</td>\n",
       "      <td>22</td>\n",
       "      <td>23</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4896</th>\n",
       "      <td>14</td>\n",
       "      <td>40</td>\n",
       "      <td>30</td>\n",
       "      <td>7</td>\n",
       "      <td>11</td>\n",
       "      <td>21</td>\n",
       "      <td>82</td>\n",
       "      <td>29</td>\n",
       "      <td>57</td>\n",
       "      <td>15</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4897</th>\n",
       "      <td>19</td>\n",
       "      <td>24</td>\n",
       "      <td>38</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>23</td>\n",
       "      <td>70</td>\n",
       "      <td>74</td>\n",
       "      <td>49</td>\n",
       "      <td>9</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4898 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Fixed acidity  Volatile acidity  Citric acid  Residual sugar  Chlorides  \\\n",
       "0                31                36           36             302         34   \n",
       "1                23                42           34              17         38   \n",
       "2                43                38           40             101         39   \n",
       "3                34                28           32             128         47   \n",
       "4                34                28           32             128         47   \n",
       "5                43                38           40             101         39   \n",
       "6                22                46           16             103         34   \n",
       "7                31                36           36             302         34   \n",
       "8                23                42           34              17         38   \n",
       "9                43                26           43              15         33   \n",
       "10               43                36           41              14         22   \n",
       "11               48                28           40              56         24   \n",
       "12               41                19           37               9         29   \n",
       "13               27                15           40              15         33   \n",
       "14               45                66           62             288         29   \n",
       "15               27                17           38              15         21   \n",
       "16               23                77            4               7         35   \n",
       "17               22               103           48               9         18   \n",
       "18               36                50           42               7         22   \n",
       "19               26                44           14             112         33   \n",
       "20               22               103           48               9         18   \n",
       "21               24                44           38              39         27   \n",
       "22               29                34           42              19         38   \n",
       "23               38               104           14              15         63   \n",
       "24               27                36           41              11         41   \n",
       "25               31                32           32             137         35   \n",
       "26               30                30           35               5         41   \n",
       "27               31                38           39             132         40   \n",
       "28               36                36           48               7         36   \n",
       "29               34                46           36              25         22   \n",
       "...             ...               ...          ...             ...        ...   \n",
       "4868             17                28           31              62         35   \n",
       "4869             27                30           33             155         21   \n",
       "4870             20                46           28              95         10   \n",
       "4871              9                22           40              23          4   \n",
       "4872             19                66           41             186         21   \n",
       "4873             16                24           32              17         19   \n",
       "4874             15                22           36              33         37   \n",
       "4875             36                26           26               9         24   \n",
       "4876             22                58           42              33         27   \n",
       "4877             18                85            0               2         21   \n",
       "4878             22                84            2               3         24   \n",
       "4879             27                50           40             121         35   \n",
       "4880             27                50           40             121         35   \n",
       "4881              9                29           27             176         19   \n",
       "4882             14                46           13              11         26   \n",
       "4883              8                75           17              23         24   \n",
       "4884             26                48           38             125         37   \n",
       "4885             27                50           40             121         35   \n",
       "4886             22                24           28              82         17   \n",
       "4887             22                64           22              23         12   \n",
       "4888             29                26           36               9         41   \n",
       "4889              8                29           27             176         19   \n",
       "4890             20                50           29              28         25   \n",
       "4891             16                24           32               3         27   \n",
       "4892             26                28           38              11         21   \n",
       "4893             22                24           29              17         28   \n",
       "4894             27                46           36             120         36   \n",
       "4895             26                30           19               9         30   \n",
       "4896             14                40           30               7         11   \n",
       "4897             19                24           38               2          9   \n",
       "\n",
       "      Free sulfur dioxide  Total sulfur dioxide  Density  pH  Sulphates  \\\n",
       "0                      57                   146      878  23         22   \n",
       "1                      13                   106      471  53         26   \n",
       "2                      33                    69      560  49         21   \n",
       "3                      59                   163      601  42         17   \n",
       "4                      59                   163      601  42         17   \n",
       "5                      33                    69      560  49         21   \n",
       "6                      33                   110      544  41         24   \n",
       "7                      57                   146      878  23         22   \n",
       "8                      13                   106      471  53         26   \n",
       "9                      30                   102      453  45         22   \n",
       "10                      9                    35      194  22         33   \n",
       "11                     17                    81      527  37         30   \n",
       "12                     16                    47      300  41         40   \n",
       "13                     60                   117      230  77         29   \n",
       "14                     49                   148      863  21         44   \n",
       "15                     30                    84      249  48         32   \n",
       "16                     33                    71      370  47         13   \n",
       "17                     32                    47       59  56         16   \n",
       "18                     17                   147      274  35         30   \n",
       "19                     38                   107      593  45         27   \n",
       "20                     32                    47       59  56         16   \n",
       "21                     19                    74      230  40         12   \n",
       "22                     49                    95      387  70         25   \n",
       "23                     27                   144      445  28         28   \n",
       "24                     16                   116      560  65         24   \n",
       "25                     72                   229      593  48         27   \n",
       "26                     39                   120      387  68         21   \n",
       "27                     36                   115      641  61         30   \n",
       "28                     17                   106      249  42         26   \n",
       "29                     42                    86      175  33         48   \n",
       "...                   ...                   ...      ...  ..        ...   \n",
       "4868                   51                    97      409  54         41   \n",
       "4869                    6                    53      650  42         28   \n",
       "4870                   32                   106      290  38         13   \n",
       "4871                   21                    70      100  60         32   \n",
       "4872                   63                   156      647  37         37   \n",
       "4873                   37                    95      160  56         29   \n",
       "4874                   16                    98      372  72         26   \n",
       "4875                   18                    69      339  35         18   \n",
       "4876                   38                    90      241  59         36   \n",
       "4877                   11                    54      375  48         13   \n",
       "4878                    4                    53      328  47         12   \n",
       "4879                   88                   146      546  38         27   \n",
       "4880                   88                   146      546  38         27   \n",
       "4881                   38                    91      585  30         27   \n",
       "4882                   57                   130      287  49         15   \n",
       "4883                   77                   122       96  50         12   \n",
       "4884                   88                   150      545  37         27   \n",
       "4885                   88                   146      546  38         27   \n",
       "4886                   57                    94      273  44         78   \n",
       "4887                    3                    28       64  27         56   \n",
       "4888                   43                   100      414  27         31   \n",
       "4889                   38                    91      585  30         27   \n",
       "4890                   27                    72       71  29         21   \n",
       "4891                   43                    94      188  47         23   \n",
       "4892                   32                    84      385  52         31   \n",
       "4893                   26                    64      224  50         27   \n",
       "4894                   73                   144      544  38         23   \n",
       "4895                   33                    83      347  22         23   \n",
       "4896                   21                    82       29  57         15   \n",
       "4897                   23                    70       74  49          9   \n",
       "\n",
       "      Alcohol  \n",
       "0           5  \n",
       "1          12  \n",
       "2          24  \n",
       "3          21  \n",
       "4          21  \n",
       "5          24  \n",
       "6          15  \n",
       "7           5  \n",
       "8          12  \n",
       "9          43  \n",
       "10         70  \n",
       "11         17  \n",
       "12         38  \n",
       "13         79  \n",
       "14         17  \n",
       "15         53  \n",
       "16         15  \n",
       "17         84  \n",
       "18         49  \n",
       "19         12  \n",
       "20         84  \n",
       "21         43  \n",
       "22         31  \n",
       "23         10  \n",
       "24         22  \n",
       "25         29  \n",
       "26         22  \n",
       "27         31  \n",
       "28         59  \n",
       "29         77  \n",
       "...       ...  \n",
       "4868       38  \n",
       "4869       20  \n",
       "4870       55  \n",
       "4871       71  \n",
       "4872       17  \n",
       "4873       67  \n",
       "4874       22  \n",
       "4875       17  \n",
       "4876       59  \n",
       "4877        5  \n",
       "4878       12  \n",
       "4879       13  \n",
       "4880       13  \n",
       "4881       11  \n",
       "4882       37  \n",
       "4883       57  \n",
       "4884       15  \n",
       "4885       14  \n",
       "4886       74  \n",
       "4887       87  \n",
       "4888        9  \n",
       "4889       11  \n",
       "4890       65  \n",
       "4891       35  \n",
       "4892       17  \n",
       "4893       47  \n",
       "4894       15  \n",
       "4895       11  \n",
       "4896       84  \n",
       "4897       65  \n",
       "\n",
       "[4898 rows x 11 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features = ['Fixed acidity','Volatile acidity','Citric acid','Residual sugar','Chlorides','Free sulfur dioxide','Total sulfur dioxide','Density','pH','Sulphates','Alcohol']\n",
    "X = df.get(features)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 6, 6, ..., 6, 7, 6])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = df['Quality'].values\n",
    "\n",
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.46268657  0.29032258  0.41860465 ...,  0.28205128  0.04901961  6.        ]\n",
      " [ 0.34328358  0.33870968  0.39534884 ...,  0.33333333  0.11764706  6.        ]\n",
      " [ 0.64179104  0.30645161  0.46511628 ...,  0.26923077  0.23529412  6.        ]\n",
      " ..., \n",
      " [ 0.3880597   0.24193548  0.22093023 ...,  0.29487179  0.10784314  6.        ]\n",
      " [ 0.20895522  0.32258065  0.34883721 ...,  0.19230769  0.82352941  7.        ]\n",
      " [ 0.28358209  0.19354839  0.44186047 ...,  0.11538462  0.6372549   6.        ]]\n"
     ]
    }
   ],
   "source": [
    "X = MinMaxScaler().fit_transform(X)\n",
    "ds = np.column_stack([X, Y])\n",
    "print(ds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train, test = train_test_split(ds, test_size = 0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3.4029300305905181, 0.80443416035269022, -0.1295338519631013, 0.69928900238763125, 0.26449097492915419, 0.2411940584024376, 0.56376844161857964, 0.20005152588438657, 0.1436303923927901, 1.1744885211676466, 0.64679462954705169, 2.0577630282783157]\n"
     ]
    }
   ],
   "source": [
    "l_rate = 0.0001\n",
    "n_epoch = 100\n",
    "\n",
    "coefs = coefficients_sgd(train, l_rate, n_epoch)\n",
    "print(coefs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = list()\n",
    "for row in test:\n",
    "    predicted = predict(row, coefs)\n",
    "    predictions.append(predicted)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_test = list()\n",
    "for i in range(1470):\n",
    "    y_test.append(test[i][11])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "predicted = predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "soma = 0\n",
    "for i in range(len(y_test)):\n",
    "    erro = predicted[i] - y_test[i]\n",
    "    soma += (erro**2)\n",
    "    erroMedio = soma/float(len(y_test))\n",
    "acc = sqrt(erroMedio)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8120004794092449"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
